package homes;

import org.hibernate.Session;
import org.hibernate.Transaction;

import servicios.Solicitud;

public class SessionManager {
	public static ThreadLocal<Session> thread=new ThreadLocal<Session>();
	
	public static Object execute(Solicitud solicitud){
		    Object result=null;
			Transaction trans=null;
			Session session=null;
			try{
		     session=Configuracion.getSession();
		     thread.set(session);
		     trans=session.beginTransaction();
		     
		     
		    result= solicitud.ejecutar();
		     
		     session.flush();
		     trans.commit();
			}catch(RuntimeException e){
				if(trans!=null){
					trans.rollback();
					//lanzar nuestra exc
				}
				throw e;
			}finally{
				if(session != null)
				session.close();
				thread.set(null);
			}
		     
		     
		     return result;
		}
       
}
